package com.miracle.common.base;

import android.support.v7.widget.RecyclerView;

import com.miracle.basemvp.base.BaseActivity;
import com.miracle.basemvp.base.IPresenter;
import com.miracle.basemvp.base.state.IPageView;
import com.miracle.common.util.CommonConstants;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

/**
 * com.miracle.common.base
 * (c)2018 AIR Times Inc. All rights reserved.
 *
 *
 * @author WangJQ
 * @version 1.0
 * @date 2018/7/18 16:38
 * @see com.miracle.common.base
 */
public abstract class BaseListActivity<P extends IPresenter, R> extends BaseActivity<P>
        implements IPageView<R>, OnLoadmoreListener, OnRefreshListener {
    protected int mCurrPage = CommonConstants.Page.FIRST_PAGE;
    protected int mPageSize = CommonConstants.Page.PAGE_SIZE_B;
    protected boolean mIsLoadMore = false;

    public abstract List<R> setList();

    public abstract RecyclerView.Adapter setAdapter();

    public abstract void requestData();

    @Override
    public void showData(R r) {

    }

    @Override
    public int getCurrPage() {
        return mCurrPage;
    }

    @Override
    public int getPageSize() {
        return mPageSize;
    }

    @Override
    public boolean isLoadMore() {
        return mIsLoadMore;
    }

    @Override
    public void getListSucc(List<R> list) {
        setList().clear();
        setList().addAll(list);
        setAdapter().notifyDataSetChanged();
    }

    @Override
    public void loadMoreSucc(List<R> list) {
        setList().addAll(list);
        setAdapter().notifyDataSetChanged();
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        mIsLoadMore = true;
        mCurrPage++;
        requestData();
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        refreshlayout.resetNoMoreData();
        mIsLoadMore = false;
        mCurrPage = CommonConstants.Page.FIRST_PAGE;
        requestData();
    }
}
